package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.AdminService;
import com.lanchetech.bean.request.AdminRolePermissionReq;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.request.SysLogReq;
import com.lanchetech.bean.request.UpdatePasswordReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.AdminDetailVO;
import com.lanchetech.entity.Admin;
import com.lanchetech.entity.AdminPermission;
import com.lanchetech.entity.AdminRole;
import com.lanchetech.entity.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "管理员信息接口", tags = "管理员信息接口")
@RequestMapping("/api/admin")
@RestController
public class AdminController {

    @Autowired
    AdminService adminService;


    @ApiOperation("获取管理员详细信息")
    @GetMapping("/getAdminDetail")
    public ResultData<AdminDetailVO> getAdminDetail(@AuthenticationPrincipal Admin admin) {
        return adminService.getAdminDetail(admin);
    }

    @ApiOperation("获取管理员权限列表")
    @GetMapping("/getAdminPermissions")
    public ResultData<List<AdminPermission>> getAdminPermissions(@AuthenticationPrincipal Admin admin) {
        return adminService.getAdminPermissions(admin);
    }

    @ApiOperation("管理员修改密码")
    @PostMapping("/updatePassword")
    public BaseResp updatePassword(@AuthenticationPrincipal Admin admin, @RequestBody UpdatePasswordReq req) {
        return adminService.updatePassword(admin, req);
    }

    @ApiOperation("获取管理员列表")
    @PreAuthorize("hasAuthority('admin.index')")
    @PostMapping("/getAdminPage")
    public ResultData<BasePageResp<Admin>> getAdminPage(@RequestBody PageReq req) {
        return adminService.getAdminPage(req);
    }

    @ApiOperation("获取全部管理员")
    @PreAuthorize("hasAuthority('admin.index')")
    @PostMapping("/getAdminList")
    public ResultData<List<Admin>> getAdminList(@RequestBody PageReq req) {
        return adminService.getAdminList(req);
    }

    @ApiOperation("获取管理员角色列表")
    @PreAuthorize("hasAuthority('admin.role')")
    @GetMapping("/getAdminRoleList")
    public ResultData<List<AdminRole>> getAdminRoleList() {
        return adminService.getAdminRoleList();
    }

    @ApiOperation("获取管理员角色分页")
    @PreAuthorize("hasAuthority('admin.role')")
    @PostMapping("/getAdminRolePage")
    public ResultData<BasePageResp<AdminRole>> getAdminRolePage(@RequestBody PageReq req) {
        return adminService.getAdminRolePage(req);
    }

    @ApiOperation("获取管理员角色拥有的权限列表")
    @PreAuthorize("hasAuthority('admin.permission')")
    @GetMapping("/getAdminRolePermissionList")
    public ResultData<List<AdminPermission>> getAdminRolePermissionList(@RequestParam("id") Long id) {
        return adminService.getAdminRolePermissionList(id);
    }

    @ApiOperation("获取管理员所有权限列表")
    @PreAuthorize("hasAuthority('admin.permission')")
    @GetMapping("/getAllPermissionList")
    public ResultData<List<AdminPermission>> getAllPermissionList() {
        return adminService.getAllPermissionList();
    }

    @OperatorLog("更新管理员角色的权限")
    @ApiOperation("更新管理员角色的权限")
    @PreAuthorize("hasAuthority('admin.role.permission.auth')")
    @PostMapping("/updateAdminRolePermission")
    public BaseResp updateAdminRolePermission(@RequestBody AdminRolePermissionReq req) {
        return adminService.updateAdminRolePermission(req);
    }

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @OperatorLog("添加或修改管理员角色")
    @ApiOperation("添加或修改管理员角色")
    @PreAuthorize("hasAuthority('admin.role.edit.auth')")
    @PostMapping("/editAdminRole")
    public BaseResp editAdminRole(@RequestBody AdminRole adminRole) {
        return adminService.editAdminRole(adminRole);
    }

    @OperatorLog("删除管理员角色")
    @ApiOperation("删除管理员角色")
    @PreAuthorize("hasAuthority('admin.role.edit.auth')")
    @GetMapping("/deleteAdminRole")
    public BaseResp deleteAdminRole(@RequestParam("id") Long id) {
        return adminService.deleteAdminRole(id);
    }

    @OperatorLog("添加或修改管理员")
    @ApiOperation("添加或修改管理员")
    @PreAuthorize("hasAuthority('admin.index.edit.auth')")
    @PostMapping("/editAdmin")
    public BaseResp editAdmin(@RequestBody Admin req, @AuthenticationPrincipal Admin admin) {
        log.info("我进入'添加或修改管理员角色'了+++++++++++++++++++++++++++++++++++++++++++++");
        return adminService.editAdmin(admin, req);
    }

    @OperatorLog("删除管理员")
    @ApiOperation("删除管理员")
    @PreAuthorize("hasAuthority('admin.index.edit.auth')")
    @GetMapping("/deleteAdmin")
    public BaseResp deleteAdmin(@RequestParam("id") Long id, @AuthenticationPrincipal Admin admin) {
        return adminService.deleteAdmin(admin, id);
    }

    @OperatorLog("重置管理员密码")
    @ApiOperation("重置管理员密码")
    @PreAuthorize("hasAuthority('admin.index.edit.auth')")
    @GetMapping("/resetAdminPassword")
    public BaseResp resetAdminPassword(@RequestParam("id") Long id) {
        return adminService.resetAdminPassword(id);
    }

    @ApiOperation("查询指定管理员详细信息")
    @PreAuthorize("hasAuthority('admin.edit')")
    @GetMapping("/queryAdminDetail")
    public ResultData<Admin> queryAdminDetail(@RequestParam("id") Long id) {
        return adminService.queryAdminDetail(id);
    }

    @ApiOperation("获取管理员操作日志分页")
    @PreAuthorize("hasAuthority('admin.sys-log')")
    @PostMapping("/getSysLogPage")
    public ResultData<BasePageResp<SysLog>> getSysLogPage(@RequestBody SysLogReq req) {
        return adminService.getSysLogPage(req);
    }

    @ApiOperation("获取权限管理员列表")
    @GetMapping("/getPermissionAdmin")
    public ResultData<List<Admin>> getPermissionAdmin(@RequestParam("id") Long id) {
        return adminService.getPermissionAdmin(id);
    }
}
